package com.pro.controller;

import com.pro.pojo.JsonData;
import com.pro.utils.FastDFSClientUtil;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/Upload")
public class FileController {

    @Autowired
    private FastDFSClientUtil fsc;

    @PostMapping("/upload")
    @ResponseBody
    public JsonData fdfsUpload(@RequestParam("file") MultipartFile file, HttpServletRequest request) {
        String resAccessUrl="";
        try {
            //数据库文件路径
            String fileUrl = fsc.uploadFile(file);
            System.out.println(fileUrl);
            //前台传递路径
            resAccessUrl = fsc.getResAccessUrl(fileUrl);

            request.setAttribute("msg", "成功上传文件，  '" + fileUrl + "'");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return resAccessUrl!=null?JsonData.buildSuccess(resAccessUrl):JsonData.buildError("服务器出错了");
    }

    @PostMapping("/uploadFiles")
    @ResponseBody
    public JsonData uploadFiles(@RequestParam("file") MultipartFile[] file, HttpServletRequest request) {
        String resAccessUrl="";
        for (MultipartFile multipartFile : file) {
            try {
                //数据库文件路径
                String fileUrl = fsc.uploadFile(multipartFile);

                resAccessUrl = fsc.getResAccessUrl(fileUrl);
                //前台传递路径
                request.setAttribute("msg", "成功上传文件，  '" + fileUrl + "'");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return resAccessUrl!=null?JsonData.buildSuccess(resAccessUrl):JsonData.buildError("服务器出错了");
    }

    @RequestMapping("/download")
    @ResponseBody
    public void download(String filePath , HttpServletRequest request , HttpServletResponse response) throws IOException {

        String[] paths = filePath.split("/");
        String groupName = null ;
        for (String item : paths) {
            if (item.indexOf("group") != -1) {
                groupName = item;
                break ;
            }
        }
        String path = filePath.substring(filePath.indexOf(groupName) + groupName.length() + 1, filePath.length());
        InputStream input = fsc.download(groupName, path);

        //根据文件名获取 MIME 类型
        String fileName = paths[paths.length-1] ;
        System.out.println("fileName :" + fileName); // wKgIZVzZEF2ATP08ABC9j8AnNSs744.jpg
        String contentType = request.getServletContext().getMimeType(fileName);
        String contentDisposition = "attachment;filename=" + fileName;

        // 设置头
        response.setHeader("Content-Type",contentType);
        response.setHeader("Content-Disposition",contentDisposition);

        // 获取绑定了客户端的流
        ServletOutputStream output = response.getOutputStream();

        // 把输入流中的数据写入到输出流中
        IOUtils.copy(input,output);
        input.close();

    }
    @RequestMapping(value = "/layeditUpload")
    @ResponseBody
    public JsonData layeditUpload(@RequestParam("file") MultipartFile file){
        Map<String, Object> map1 = new HashMap<>();
        String layeditUploadUrl="";
        try {
            String f = fsc.uploadFile(file);
            layeditUploadUrl = fsc.getResAccessUrl(f);

            map1.put("src",layeditUploadUrl);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return map1 != null?JsonData.buildSuccess(map1):JsonData.buildError("文件上传失败");
    }

}
